from django.db import models
from mptt.models import MPTTModel, TreeForeignKey
from app.models import BaseModel  # 假设 BaseModel 已定义


class ToDoTags(BaseModel):
    name = models.CharField(
        max_length=255,
        verbose_name="todo标签名称",
        db_comment="todo标签名称",
        null=True,
        blank=True
    )
    parent = models.CharField(
        max_length=255,
        verbose_name="todo标签父节点",
        db_comment="todo标签父节点",
        null=True,
        blank=True
    )

    def __str__(self):
        return self.name or "未命名标签"

    class Meta:
        db_table = "todolist_tags"
        verbose_name = "待办标签"
        verbose_name_plural = "02.待办标签"
        db_table_comment = "待办标签表"


class TodoList(BaseModel, MPTTModel):
    name = models.CharField(
        max_length=255,
        verbose_name="todo名称",
        null=True,
        blank=True
    )
    parent = TreeForeignKey(
        'self',
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        related_name='children',
        db_comment="todo父节点"
    )
    begin_at = models.DateTimeField(
        null=True, blank=True, verbose_name="开始时间", db_comment="开始时间")
    end_at = models.DateTimeField(
        null=True, blank=True, verbose_name="结束时间", db_comment="结束时间")
    tag_ids = models.CharField(
        max_length=255,
        verbose_name="todo标签ID",
        blank=True, null=True
        # 不要加 db_comment
    )

    # 可选：显式声明 MPTT 字段以避免迁移问题
    level = models.PositiveIntegerField(
        default=0, editable=False, db_index=True)
    lft = models.PositiveIntegerField(default=0, editable=False, db_index=True)
    rght = models.PositiveIntegerField(
        default=0, editable=False, db_index=True)
    tree_id = models.PositiveIntegerField(
        default=0, editable=False, db_index=True)

    def __str__(self):
        indent = "-" * self.level
        return f"{indent}{self.name}" if self.name else f"{indent}未命名"

    class Meta:
        db_table = "todolist_list"
        verbose_name = "待办列表"
        verbose_name_plural = "01.待办列表"
        db_table_comment = "待办表"
